#! /usr/bin/perl

#-------------------------------------------------
# Script to parsing GEAPHYSVBO input data.
#-------------------------------------------------

use strict;
use Time::Local;


#---------------------------------------------------
# Fields of table 'read_insurance0_new_t' in PayorIntel 
#---------------------------------------------------
our @payor_read_ins1 = qw(
  cust_id
  treatment_code
  output_id
  hid
  acctnum
  cpi
  client_npi1
  npi1
  tax_id1
  legacy1
  pin1
  entity_name1
  entity_pay_to_street1
  entity_pay_to_city1
  entity_pay_to_state1
  entity_pay_to_zip1
  entity_information1
  entity_phone1
  lname
  mname
  fname
  addr1
  addr2
  city
  state
  zip5
  zip4
  ssn
  gender
  dob
  g_lname
  g_mname
  g_fname
  g_addr1
  g_addr2
  g_city
  g_state
  g_zip5
  g_zip4
  g_ssn
  g_gender
  g_dob
  relationship_flag
  g_employ_name
  provider_state
  service_date_begin
  service_date_end
  pat_proc_type
  pat_type
  cpt
  drg
  physician_code
  charges
  balance
  acct_status
  client_field1
  client_field2
  client_field3
  client_date1
);


#----------------------------------------------
# Processing input file or input file handle
#----------------------------------------------
our $indata = $ARGV[0] || "-";
our $INFD = undef;

our %dest;
our $bill_ins_pmt ="";

if( $indata ne "-" ) {
	if ( ! -r $indata ) {
		print STDERR " file $indata not accessable !!!\n";
		exit 1;
	}
	else {
		open(GEA, "<$indata");
		$INFD = *GEA;
	}
} else {
	$INFD = *STDIN;
}



#----------------------------------------------------
# Skip remark lines at the beginning of the data file
#----------------------------------------------------
while(my $line = <$INFD> ) {
	last if $line =~ /RG-COMPLETE\s+DETAIL/;
}


#-------------------------------------------------
# GEA specific values
#-------------------------------------------------
$dest{cust_id}		=33;
$dest{output_id}	=1;
$dest{treatment_code}	=1;
$dest{client_npi1}	=1;

while(my $line = <$INFD> ) {
	if ($line =~ /RG-COMPLETE\s+DETAIL/) {
		if( $bill_ins_pmt =~ /^$/) {
			print_record(\%dest);
		} 
		;#else { print STDERR "Invalid $dest{acctnum} \n"; }
		%dest=();
		$dest{cust_id}		=33;
		$dest{output_id}	=1;
		$dest{treatment_code}	=1;
		$dest{client_npi1}	=1;
		$bill_ins_pmt="";
		next;
	}

	chomp $line;
	$line =~ s/[^\x20-\x7e]//g;
	next if $line =~ /^$/;
	if( $line =~ /NAME.+SOC.+ACCOUNT#/ ) {
		my $acct=$line;
		$acct =~ s/^.*ACCOUNT#\s*\.+//;
		$acct =~ s/\s//g;
		$dest{acctnum} = $acct;
		$line =~ s/\s*ACCOUNT.*$//;
		my $ssn =$line;
		$ssn =~ s/^.*SEC\s*\.*//;
		$ssn =~ s/\D//g;
		$dest{ssn} = $ssn;
		$line =~ s/\s*SOC.\s*SEC.*$//;
		my $name = $line;
		$name =~ s/^.*NAME\s*\.+//;
		if( $name =~ / (JR|Jr|SR|Sr|IV|III|II)/ ) {
			$name =~ s/ (JR|Jr|SR|Sr|IV|III|II)/ /;
		}
		my @nms = split(/\s+/, $name);
		$dest{lname} = $nms[0];
		$dest{fname} = $nms[1];
		if ( @nms == 3 ) {
		  $dest{mname} = $nms[2];
		}
		;#print STDERR "$nms[1], $nms[0], $nms[2]\n";
	}
	elsif( $line =~ /ADDRESS.+ADDRESS.+DOB/ ) {
		my $dob = $line;
		$dob =~ s/^.*DOB\s*\.+//;
		$dob =~ s/\s//g;
		$dest{dob} = format_dob( $dob );
		$line =~ s/\s*DOB.*$//;
		my $addr1 = $line;
		$addr1 =~ s/\s*ADDRESS\s+2.*$//;
		$addr1 =~ s/^.*ADDRESS\s+1\.+//;
		$dest{addr1} = $addr1;
		my $addr2 = $line;
		$addr2 =~ s/^.*ADDRESS\s+2\.+//;
		$dest{addr2} = $addr2;
	}
	elsif( $line =~ /CITY.*STATE.*ZIP.*SEX/ ) {
		my $gender = $line;
		$gender =~ s/^.*SEX\s*\.//;
		$gender =~ s/\s//g;
		$dest{gender} = $gender;
		$line =~ s/\s*SEX.*$//;
		my $city = $line;
		$city =~ s/\s+STATE.*$//;
		$city =~ s/^\s*CITY\s*\.*//;
		$dest{city} = $city;
		$line =~ s/^.*\sSTATE/STATE/;
		my $state = $line;
		$state =~ s/\s+ZIP.*$//;
		$state =~ s/^.*STATE\s*\.+//;
		$dest{state} = $state;
		my $zip = $line;
		$zip =~ s/^.*ZIP\s*\.*//;
		$zip =~ s/\s//g;
		$zip =~ s/-//g;
		if ( $zip =~ /\d{9}/ ) { 
			$zip =~ m/(\d{5})\d{4}/;
			$dest{zip5} = $1;
			$zip =~ m/\d{5}(\d{4})/;
			$dest{zip4} = $1;
		}
		elsif ( $zip =~ /(\d{5})/ ) { 
			$dest{zip5} = $1;
			$dest{zip4} = "";
		}
	}
	elsif( $line =~ /DM\s+RECALL/ ) {
		my $headline = $line;
		$line = <$INFD>;
		chomp $line;
		my @chars = split(//, $line);
		my $x=$headline;
		$x =~ s/BILL-INSURANCE-PMT.*$//;
		my $st = length($x);
		my $end = $st + length("BILL-INSURANCE-PMT");
		for ( my $i=$st; $i<$end; $i++) {
			if ($chars[$i] =~ /\d/) {
				$bill_ins_pmt = "1";
				last;
			}
		}
		if( ! $bill_ins_pmt ) {
			$x=$headline;
			$x =~ s/ADMITTED.*$//;
			$st = length($x);
			$end = $st + length("ADMITTED");
			my $sdb="";
			for ( my $i=$st; $i<$end; $i++) {
				$sdb .= $chars[$i];
			}
			$sdb =~ s/\s//g;
			if( $sdb =~ m{\d\d/\d\d/(\d\d)$} ) {
				my $yr = $1;
				$yr += 2000;
				$sdb =~ s/\d\d$/$yr/;
			}
			$dest{service_date_begin} = $sdb;
		}
	}	
	elsif( $line =~ /SERVICE\s+POSTING/ ) {
		$line = <$INFD>;
		my $bal;
		while( $line = <$INFD> ) {
			last if $line =~ /\.{32}$/; 
			chomp $line;
			if ( $line =~ /^.*\s(\d+\.\d{2})\s*$/ ){
			   $line =~ s/^.*\s(\d+\.\d{2})\s*$/$1/; 
			   $bal = $line;
			}
		}
		$dest{balance}=$bal;
	}
	
}

if( $bill_ins_pmt =~ /^$/) {
	print_record(\%dest);
}

exit 0;


sub print_record($){
	my $hr = shift;
	#------------------------------------------
	# Cases not to print record.
	#------------------------------------------
	return if $hr->{balance} == 0.00;
	return if $hr->{service_date_begin} !~ /\d/;
	return if length($hr->{acctnum} ) < 7;

	$hr->{provider_state} = "GA";
	if( $hr->{acctnum} =~ /(^J.+C$)|(^V.+C$)|(^8.+B$)/) {
		if( $hr->{state} =~ /SC/) { 
			$hr->{provider_state} = "SC";
		} 
	} 

	foreach my $fld ( @payor_read_ins1 ) {
		my $x = $hr->{$fld};
		$x =~ s/^\s*//;
		$x =~ s/\s*$//;
		$x =~ s/\|//;
		print "$x";
		print "|" if $fld ne $payor_read_ins1[$#payor_read_ins1];
	}
	print "\n";
}

sub format_dob($) {
	my $date = shift;
	my $ret = $date;
	if( $date =~ m{^(\d\d)/(\d\d)/(\d\d)$} ) {
		my $m = $1;
		my $d = $2;
		my $y = $3;
		
		$d =~ s/^0//;
		$m =~ s/^0//;
		$y =~ s/^0//;

		my ($nd, $nm, $ny) = (localtime)[3,4,5];
		if ( 100+$y > $ny  ) {	
				$ret = sprintf("%02d/%02d/%02d", $m, $d, 1900+$y);
		} else {
				$ret = sprintf("%02d/%02d/%02d", $m, $d, 2000+$y);
		}
	}
	$ret;
}

